﻿#pragma once
#ifndef XL_XEXPAND_DWEBX_PACKINGCLASS_H
#define XL_XEXPAND_DWEBX_PACKINGCLASS_H

struct WoHttpControllerModel;

#include "oatpp/web/server/HttpRequestHandler.hpp"

class WoRequestHandler : public oatpp::web::server::HttpRequestHandler
{
private:
	std::vector<WoHttpControllerModel> m_CallFunc;
public:
	WoRequestHandler(WoHttpControllerModel& CallFunc);
	WoRequestHandler(CXVector<WoHttpControllerModel>& CallFuncs);
	std::shared_ptr<OutgoingResponse> handle(const std::shared_ptr<IncomingRequest>& request) override;
	const WoHttpControllerModel& GetEndHandler();
	const std::vector<WoHttpControllerModel>& GetHandlers();
};

class WoOutgoingBody : public oatpp::web::protocol::http::outgoing::Body {
private:
	std::vector<char> m_data;
public:
	WoOutgoingBody() { m_data.reserve(1024); };
	WoOutgoingBody(const char* data) {
		m_data.reserve(1024);
		auto num = strlen(data);
		m_data.insert(m_data.end(), data, data+ num);
	};
	operator oatpp::web::protocol::http::outgoing::Body*() {
		return (oatpp::web::protocol::http::outgoing::Body*)this;
	};
	void declareHeaders(Headers& headers) override;
	p_char8 getKnownData() override;
	v_int64 getKnownSize() override;
	oatpp::v_io_size read(void* buffer, v_buff_size count, oatpp::async::Action& action) override;
	void AddByte(const char*data,UINT size);
	void AddTextA(const char* str) { 
		auto num = strlen(str);
		m_data.insert(m_data.end(), str, str + num);
	};
	void SetByte(const char* data, UINT size) { 
		m_data.clear();
		m_data.insert(m_data.end(), data, data + size);
		//m_data.set((void*)data, size);
	};
	void SetTextA(const char* str) { 
		m_data.clear();
		auto num = strlen(str);
		m_data.insert(m_data.end(), str, str + num);
	};
	UINT size();
};


//欺骗编译器的伪装类(骚活  (笑
class WoOutgoingBufferBody : public oatpp::base::Countable, public oatpp::web::protocol::http::outgoing::Body {
public:
	oatpp::String m_buffer;
	oatpp::data::share::StringKeyLabel m_contentType;
	oatpp::data::buffer::InlineReadData m_inlineData;
public:
	WoOutgoingBufferBody(const oatpp::String& buffer, const oatpp::data::share::StringKeyLabel& contentType);
public:

	/**
	 * Create shared BufferBody.
	 * @param buffer - &id:oatpp::String;.
	 * @param contentType - type of the content.
	 * @return - `std::shared_ptr` to BufferBody.
	 */
	static std::shared_ptr<WoOutgoingBufferBody> createShared(const oatpp::String& buffer,
		const oatpp::data::share::StringKeyLabel& contentType = oatpp::data::share::StringKeyLabel());

	/**
	 * Read operation callback.
	 * @param buffer - pointer to buffer.
	 * @param count - size of the buffer in bytes.
	 * @param action - async specific action. If action is NOT &id:oatpp::async::Action::TYPE_NONE;, then
	 * caller MUST return this action on coroutine iteration.
	 * @return - actual number of bytes written to buffer. 0 - to indicate end-of-file.
	 */
	oatpp::v_io_size read(void* buffer, v_buff_size count, oatpp::async::Action& action) override;

	/**
	 * Declare `Content-Length` header.
	 * @param headers - &id:oatpp::web::protocol::http::Headers;.
	 */
	void declareHeaders(Headers& headers) override;

	/**
	 * Pointer to the body known data.
	 * @return - `p_char8`.
	 */
	p_char8 getKnownData() override;

	/**
	 * Return known size of the body.
	 * @return - `v_buff_size`.
	 */
	v_int64 getKnownSize() override;

};



#endif

